<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Delete Schedule</title>
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
</head>
<body>
<%

	//get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//GET PARAMETE FROM HIDDEN
			String checker2 = request.getParameter("checker2");
			int CurriculumId = Integer.parseInt(request.getParameter("CurriculumId"));
			String nameCurriculumEN = request.getParameter("nameCurriculumEN");
			String departmentName = new String(request.getParameter("departmentName").getBytes("ISO8859_1"),"UTF-8");
			int departmentId = Integer.parseInt(request.getParameter("departmentId"));
			int year = Integer.parseInt(request.getParameter("year"));
			int credit = Integer.parseInt(request.getParameter("credit"));
			
			int YearSemester = Integer.parseInt(request.getParameter("YearSemester"));
			int TermSemester = Integer.parseInt(request.getParameter("TermSemester"));
			int CreditSemester = Integer.parseInt(request.getParameter("CreditSemester"));
	
	System.out.println("delete : "+CurriculumId);////////////try//////////
	System.out.println("delete YearSemester: "+YearSemester);////////////try//////////
	System.out.println("delete TermSemester: "+TermSemester);////////////try//////////
	
	//GET PERMISSION
		String name = String.valueOf(session.getAttribute("name"));
		String surname = String.valueOf(session.getAttribute("surname"));
		String role_name = String.valueOf(session.getAttribute("curriculum_role"));
		
		String department_id = String.valueOf(session.getAttribute("department_id"));
				
		System.out.println("name : " + name);///////////////////////////////
		System.out.println("department_id : " + department_id);/////////////////////////
		
		List<String> system_name = new ArrayList<String>();
		
		Permission permission = new Permission(strUser,name);
		permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
		permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
		permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
		permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
		
		String permiss = "";
		if(permission.getCurriculumCreate()) {
			permiss = permiss + "Add";
		}
		if(permission.getCurriculumUpdate()) {
			if("".equals(permiss)) {
				permiss = permiss + "Edit";
			} else {
				permiss = permiss + " Edit";
			}
		}
		if(permission.getCurriculumRead()) {
			if("".equals(permiss)) {
				permiss = permiss + "Read";
			} else {
				permiss = permiss + " Read";
			}
		}
		if(permission.getCurriculumDelete()) {
			if("".equals(permiss)) {
				permiss = permiss + "Delete";
			} else {
				permiss = permiss + " Delete";
			}
		}

		//check permission
			if(!permission.getCurriculumCreate()){%>
				<script>alert("This user dont have PERMISSION to access this zone.");</script>
				<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
			<%}
		
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	
	List<SemesterCourse> semester_course = new ArrayList<SemesterCourse>();
	try {			
	/*Find Semester in DB*/
	System.out.println("ENTER PHASE 1 ADD CATEGORY");
	
	ResultSet rs_Semester = connect.createStatement().executeQuery("SELECT S.year, S.semester, S.semester_program_id, S.curriculum_id, S.credit,"
																	+ "C.semester_program_id, C.course_id, C.course_name, C.credit, C.course_id_free_elective "
			+ " FROM semester_program S, course_semester C"
			+ " WHERE curriculum_id="+ CurriculumId + " AND S.semester_program_id=C.semester_program_id AND S.year="+YearSemester+" AND S.semester="+TermSemester+""
			+ " ORDER BY S.year" );
	
	//Query Previous Information from DB
	while(rs_Semester.next()) {
		SemesterCourse sem_cou = new SemesterCourse();
		
		sem_cou.setCourseId(rs_Semester.getString("C.course_id"));
		sem_cou.setCourseName(rs_Semester.getString("C.course_name"));
		sem_cou.setCredit(rs_Semester.getInt("C.credit"));
		sem_cou.setElective(rs_Semester.getString("C.course_id_free_elective"));
		sem_cou.setSemesterId(rs_Semester.getInt("C.semester_program_id"));
		
		semester_course.add(sem_cou);
		System.out.println("semester_course.size=  "+semester_course.size());
	}
			
	}catch (SQLException e) {
		e.printStackTrace();
	}
	
	
	
	
	//delete semester_program in database
	try {		
			connect.createStatement().executeUpdate("DELETE FROM `semester_program` "
					+" WHERE curriculum_id = '"+ CurriculumId + "' AND semester='"+TermSemester+"' AND year='"+YearSemester+"'");
			
			String sql_semester_program = "DELETE FROM `semester_program` "
					+" WHERE curriculum_id = '"+ CurriculumId + "' AND semester='"+TermSemester+"' AND year='"+YearSemester+"'";
					
			
			/* Log file */
			String log_user = strUser+" delete semester_program";
			Log.writeUserFile(log_user);
			Log.writeUserFile(sql_semester_program);
			
			if(semester_course.size() == 0){}
			else{
				connect.createStatement().executeUpdate("DELETE FROM `course_semester` "
						+" WHERE semester_program_id='"+semester_course.get(0).getSemesterId()+"'");
				
				String sql_course_semester = "DELETE FROM `course_semester` "
						+" WHERE semester_program_id='"+semester_course.get(0).getSemesterId()+"'";
						
				
				/* Log file */
				String log_user2 = strUser+" delete course_semester";
				Log.writeUserFile(log_user2);
				Log.writeUserFile(sql_course_semester);
			}		
			System.out.println("delete sucess");////////////try//////////
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	connect.close();
%>
<div id="main-content-ac" align="center" >
		<table>
		<tr>
			<font size="5">คุณได้ลบแผนการศึกษาชั้นปีที่ <%=YearSemester %> เทอมที่ <%=TermSemester %>  เสร็จสิ้น</font>
			<br><br>
			<font size="5">กรุณากดปุ่ม Back เพื่อกลับไปหน้าที่แล้ว</font>
		</tr>
		</table>
		<br>		
		<form id="form" name="form" method="post" action="curriculum_add_schedule.jsp">
		
					<input type="hidden" id="CurriculumId" name="CurriculumId" value="<%=CurriculumId%>" />
					<input type="hidden" id="checker" name="checker" value="none" />	
					<input type="hidden" id="checker2" name="checker2" value="none" />	
					
					<input type="hidden" id="nameCurriculumEN" name="nameCurriculumEN" value="<%=nameCurriculumEN%>" />
					<input type="hidden" id="departmentId" name="departmentId" value="<%=departmentId%>" />
					<input type="hidden" id="departmentName" name="departmentName" value="<%=departmentName%>" />
					<input type="hidden" id="year" name="year" value="<%=year%>" />						
					<input type="hidden" id="credit" name="credit" value="<%=credit%>" />
										
					<input type="hidden" id="YearSemester" name="YearSemester" value="<%=YearSemester%>" />	
					<input type="hidden" id="TermSemester" name="TermSemester" value="<%=TermSemester%>" />		
					<input type="hidden" id="CreditSemester" name="CreditSemester" value="<%=CreditSemester%>" />
					
					<input type="hidden" id="delete" name="delete" value="none" />
										
			<input type="submit" value="back" name="back" class="buttonStyle"/>
		
		</form>	




</div>
</body>
</html>